
import { defineComponent, h } from 'vue';
import { format } from 'date-fns';
import { Toast } from 'tdesign-mobile-vue';
import { ErrorCircleIcon } from 'tdesign-icons-vue-next';
import CalendarConverter from 'js-calendar-converter';
import { LSRUtils, } from '../../../utils/LSRUtils';
import { TDate, DizhIKeys,WuXingKeys,  } from '../../../model/LSRModel';
import { UtilsCore } from '../../../utils/UtilsCore';
import { useRouter } from 'vue-router';

export default defineComponent({
  name: "LesserSixRents",
  props: {

    // 如果有父组件传递的属性，在这里定义
  },
  setup() {+
    console.log("setup");
    // 这里可以保留 setup 函数中定义的逻辑
    // 但需要转换为 Options API 格式
    let OneTimes: number = 0;
    let TwoTimes: number = 0;
    const router = useRouter();
    return { OneTimes: OneTimes, TwoTimes: TwoTimes,router:router  };
  },
  data() {
    let date = new Date();
    let shijian = format(date, 'HH:mm');
 
    let minDate = new Date(date.getTime() - 30*60*60*24*1000);
    let maxDate = new Date(date.getTime() + 30*60*60*24*1000);
  
    return {
      numValue: null,
      textValue:null,
      divinationValue: [3],
      divinationTypeOptions: [
        {
          label: '日期起课',
          value: 1,
        },
        {
          label: '时刻起课',
          value: 2,
        },
        {
          label: '数字起课',
          value: 3,
        }
      ],
      // earthlyBranches: LSRUtils.earthlyBranches,
      minDate:minDate,
      maxData:maxDate,
      sixKeysEle: LSRUtils.getLiuRen(),
      WuXingKeys:WuXingKeys,
      visibleCalendar: false,
      visibleDatePicker: false,
      visibleDivination: false,
      dateValue: date,
      pickerValue: shijian,
      isDivination: false,
   //   isStart: false,
      isShow: false,



    };
  },
  methods: {

    formatDateCalendar(day: TDate) {
      const { date } = day;
      const year = date.getFullYear();
      const month = date.getMonth() + 1;
      const curDate = date.getDate();
      const lunar = CalendarConverter.solar2lunar(year, month, curDate);
      day.prefix = lunar.gzMonth + lunar.gzDay;
      day.suffix = lunar.IMonthCn + lunar.IDayCn;
   //   console.log(lunar);
      if (lunar.lunarFestival != null) {
        day.suffix = lunar.lunarFestival;
        day.className = 'is-holiday';
      }
      return day;
    },
    formatDateDatePicker(type: string, value: number) {
      if (type == 'hour') {
        let currentBranchIndex = LSRUtils.getShichen(value);
        let shichen = DizhIKeys[currentBranchIndex] + "时"
        return value + "点" + shichen;
      }
      else if (type == 'minute') {
        return value + "分";
      }
      return value + "秒";
    },
    onConfirmCalendar(date: Date) {
      this.dateValue = date;
    },
    onConfirmDatePicker(value: string) {
      this.pickerValue = value;
      this.visibleDatePicker = false;
    },
    onConfirmDivination(value: Array<number>) {
      this.divinationValue = value;

      this.isDivination= false;
    },

    // 如果需要，将 setup 中的 computed 属性转换到此处
    dateFormatCalendar(date: Date) {
      this.dateValue = date;
      if (date != null) {
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const curDate = date.getDate();
        const lunar = CalendarConverter.solar2lunar(year, month, curDate);

        let glDate = format(date, 'yyyy-MM-dd');//  lunar.lunarDate;
        //  lunar.gzYear+ lunar.gzMonth+lunar.gzDay  lunar.IMonthCn lunar.IDayCn

        return glDate + "(" + lunar.IMonthCn + lunar.IDayCn + ")";
      }
    },
    dateFormatDatePicker(value: string) {
      if (value != null && value.split(':').length == 2) {
        let currentBranchIndex = LSRUtils.getShichen(parseInt(value.split(':')[0]));
        let shichen = DizhIKeys[currentBranchIndex] + "时" 
        let moment = LSRUtils.getShike(parseInt(value.split(':')[0]), parseInt(value.split(':')[1])); 
        let shike = DizhIKeys[moment-1] + "刻";


        return value.split(':')[0] + "点" + value.split(':')[1] + "分 (" + shichen + shike + ")";
      }
      return "选择时刻";
    },
    dateFormatDivination(value: Array<number>) {
      this.visibleDivination = false;
      if (value != null) {
        let showStr = "";
        value.forEach(key => {
          const item = this.divinationTypeOptions.find(item =>
            item.value === key
          );
          showStr = item ? item.label : "默认值"; // 使用三元运算符设置默认值
          // 执行需要 showStr 的操作
        });

        return showStr;
      }
      return "选择起课方式";
    },
    onClickDivination() {
      let $this = this;
      this.isDivination = true;
      clearTimeout(this.OneTimes);
      clearTimeout(this.TwoTimes); 
      $this.sixKeysEle.forEach((item) => {
        item.One = false;
        item.Two = false;
        item.OneName = "";
        item.TwoName = "";
      });


      let one = 0;
      let two = 0;
      let divination=0;
      let shichen=0;//时辰落宫
    //  let stars = 0;
      if (UtilsCore.isNotEmpty(this.divinationValue)) {

        if (this.divinationValue.indexOf(1) > -1 &&
          UtilsCore.isNotEmpty(this.dateValue) &&
          UtilsCore.isNotEmpty(this.pickerValue)) {
          const year = this.dateValue.getFullYear();
          const month = this.dateValue.getMonth() + 1;
          const curDate = this.dateValue.getDate();
          const lunar = CalendarConverter.solar2lunar(year, month, curDate);
          if (this.pickerValue != null && this.pickerValue.split(':').length == 2) {
            let chen = LSRUtils.getShichen(parseInt(this.pickerValue.split(':')[0]));
            console.log(lunar.lDay + "<>" + chen);
            one = (lunar.lDay - 1) % 6;
            two = (lunar.lDay - 1 + chen) % 6;
             divination =1;
             shichen = chen;
       //      stars = lunar.lDay;
          }
          else {
            Toast({
              icon: () => h(ErrorCircleIcon),
              direction: 'column',
              message: '请补充完整日期和时辰!',
            });
            return false;
          }
        } else if (this.divinationValue.indexOf(2) > -1) {

          if (this.pickerValue != null && this.pickerValue.split(':').length == 2) {
            let chen = LSRUtils.getShichen(parseInt(this.pickerValue.split(':')[0]));
            let moment = LSRUtils.getShike(parseInt(this.pickerValue.split(':')[0]), parseInt(this.pickerValue.split(':')[1]));
          console.log("时"+chen + "<>" + '刻'+ moment);
            one = chen % 6;;
            two = (chen + moment-1) % 6;
            divination=2;
            shichen = moment-1;
       //     stars =moment;
          }
          else {
            Toast({
              icon: () => h(ErrorCircleIcon),
              direction: 'column',
              message: '请补充完整时辰与时刻',
            });
            return false;
          }
        }
        else if (this.divinationValue.indexOf(3) > -1) {
          if (this.pickerValue != null && this.pickerValue.split(':').length == 2 && this.numValue != null&&this.numValue != "") {
            let chen = LSRUtils.getShichen(parseInt(this.pickerValue.split(':')[0]));
            console.log(this.numValue + "<>" + chen);
            one = (this.numValue-1) % 6;
            two = (this.numValue-1 + chen) % 6;
            divination=3;
            //stars =this.numValue;
            shichen = chen;
          
          }
          else {
            Toast({
              icon: () => h(ErrorCircleIcon),
              direction: 'column',
              message: '请补充完整数字与时辰',
            });
            return false;
          }
        }
        else{

        }
  

         // params 传参
        // this.router.push({
        //   name: 'lsrview',
        //   params: {shichen:shichen,one:one,two:two,divination:divination }
        // });

        // query 传参
        // this.router.push({
        //   path: '/lessersixrens/view',
        //   query: {shichen:shichen,one:one,two:two,divination:divination }
        // });
    
        // state 传参  this.numValue 
        let params ={shichen:shichen,one:one,two:two,
          divination:divination,
          textValue:this.textValue,
          dateValue:format(this.dateValue, 'yyyy-MM-dd') ,pickerValue:this.pickerValue,numValue:this.numValue };
          this.router.push({
          name: 'lsrview',
          state: {params}
        });

        


      //  console.log(this.router);
        // $this.isShow = true;
        // $this.isStart = true;
        // setTimeout(() => {
        //   $this.isShow = false;
        //   $this.OneTimes = setTimeout(() => { 
        //   let dizhi:DiZhi = LSRUtils.getDizhi(shichen); 
        //    console.log("one=>"+one+"two=>"+two); 
        //     $this.sixKeysEle.forEach((item, index) => {
        //     //第一次落宫y
        //       if (item.index == one) { 
        //         item.One = true;
        //         item['OneName']=divination==1?'日':divination==2?"时":"数"; 
        //       //   if(divination===2)//时辰
        //       //   {
        //       //      item.dizhi =dizhi;  
        //       //      console.log("第一次落宫=>"+shichen);
        //       //      console.log("落宫位置:");
        //       //      console.log(item);
        //       //      LSRUtils.GroundExhaustBranch($this.sixKeysEle,LSRUtils.DiZhiList(),item.index,shichen);  
        //       //      LSRUtils.PleaseSixGods($this.sixKeysEle,SixGodKeys,item.dizhi.sixRen,0);
        //       //      LSRUtils.SixRelationsLocation($this.sixKeysEle,LSRUtils.getMutualRestraint(),index); 

        //       //   }
        //       //  else 
        //       if(divination!==2)
        //        {
        //             LSRUtils.UpFiveStars($this.sixKeysEle,FiveStarsKeys,item.index,0);   
        //         }
        //       }  
        //       //第二次落宫
        //       if (item.index == two) {
        //         item.Two = true;
        //         item['TwoName']=divination==2?"刻":"时";

        //        // if(divination!==2)//时辰
        //       //    {
    

        //             item.dizhi =dizhi; 
        //             console.log("第二次落宫=>"+shichen);
        //             console.log("落宫位置:");
        //             console.log(item);
        //             //地支
        //             LSRUtils.GroundExhaustBranch($this.sixKeysEle,LSRUtils.DiZhiList(),item.index,shichen);
        //             //六神
        //       LSRUtils.PleaseSixGods($this.sixKeysEle,SixGodKeys,item.dizhi.sixRen,0);
        //             //六亲
        //            LSRUtils.SixRelationsLocation($this.sixKeysEle,LSRUtils.getMutualRestraint(),index); 
        //    //    } else

        //         if(divination===2)
        //         {
        //         //  五星
        //            LSRUtils.UpFiveStars($this.sixKeysEle,FiveStarsKeys,item.index,0);    
        //         }
        //       }


        //     });
        //     console.log( $this.sixKeysEle);
        //     $this.isStart = false;
        //   }, 0);
        // }, 0); 

      }

    },
    onReset()
    {
       this.textValue=null;
       this.numValue=null;
       this.divinationValue=[1];
       let date = new Date();
       let shijian = format(date, 'HH:mm'); 
       this.pickerValue=shijian;
       this.dateValue=date;
    },
    onClickViewList(){ 
        this.router.push({
        name: 'lsrlist',
        
      });
    }
  },
  computed: {

    // 如果需要，将 setup 中的 computed 属性转换到此处

  },
  mounted() { 
    console.log("mounted");
    // 如果需要，将 setup 中的 onMounted 逻辑转换到此处
  }
});